package com.wclass.interceptor;


import com.wclass.service.IBaseAppUserService;
import com.wclass.common.ReturnObject;
import com.wclass.entity.BaseAppUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 权限(Token)验证
 */
@Component
public class AuthorizationInterceptor extends HandlerInterceptorAdapter {
    @Autowired
    private IBaseAppUserService iBaseAppUserService;

    public static final String LOGIN_TOKEN_KEY = "Token";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws
            Exception {
        IgnoreAuth annotation;

        //从header中获取token
        String token = request.getHeader(LOGIN_TOKEN_KEY);
        //从header中获取deviceToken,cId,mobileType
        String deviceToken = request.getHeader("deviceToken");
        String cId = request.getHeader("cId");
        String mobileType = request.getHeader("mobileType");

        //如果header中不存在token，则从参数中获取token
        if (StringUtils.isBlank(token)) {
            token = request.getParameter(LOGIN_TOKEN_KEY);
        }

        //token为空
        if (StringUtils.isBlank(token)) {
            if (handler instanceof HandlerMethod) {
                annotation = ((HandlerMethod) handler).getMethodAnnotation(IgnoreAuth.class);
            } else {
                return super.preHandle(request, response, handler);
            }
            //如果有@IgnoreAuth注解，则不验证token
            if (annotation != null) {
                return super.preHandle(request, response, handler);
            }
            SendMsgUtil.sendJsonMessage(response, new ReturnObject<>(401, "请先登录", ""));
            return false;
        } else {
            BaseAppUser baseUsers = iBaseAppUserService.selectByToken(token);
            if (baseUsers == null) {
                SendMsgUtil.sendJsonMessage(response, new ReturnObject<>(401, "请先登录", ""));
                return false;
            } else {
                BaseContextHandler.setUserID(baseUsers.getId().toString());
                BaseContextHandler.setUsername(baseUsers.getUsername());
                BaseContextHandler.setName(baseUsers.getNickName());
                //同账号多平台登录
                if (handler instanceof HandlerMethod) {
                    annotation = ((HandlerMethod) handler).getMethodAnnotation(IgnoreAuth.class);
                } else {
                    return super.preHandle(request, response, handler);
                }
                //如果有@IgnoreAuth注解，mobileType赋值为null，不进行判断
                if (annotation != null) {
                    mobileType = null;
                }
                if (StringUtils.isNotBlank(mobileType)) {
                    if (mobileType.equals(baseUsers.getMobileType()) && !mobileType.equals("web")) {
                        if (mobileType.equals("android") && StringUtils.isNotBlank(cId)) {
                            if (!cId.equals(baseUsers.getCId())) {
                                SendMsgUtil.sendJsonMessage(response, new ReturnObject<>(401, "账号已在其他平台登录，请重新登录", ""));
                                return false;
                            }
                        } else if (mobileType.equals("ios") && StringUtils.isNotBlank(deviceToken)) {
                            if (!deviceToken.equals(baseUsers.getDeviceToken())) {
                                SendMsgUtil.sendJsonMessage(response, new ReturnObject<>(401, "账号已在其他平台登录，请重新登录", ""));
                                return false;
                            }
                        }
                    } else if (!mobileType.equals(baseUsers.getMobileType())) {
                        SendMsgUtil.sendJsonMessage(response, new ReturnObject<>(401, "账号已在其他平台登录，请重新登录", ""));
                        return false;
                    }
                }
            }
        }
        return super.preHandle(request, response, handler);
    }
}
